package com.stox.services.data.provider.repository.file;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.stox.services.core.repository.AbstractCachingFileRepository;
import com.stox.services.data.provider.repository.ScripRepository;
import com.stox.shared.core.model.Scrip;
import com.stox.shared.core.model.filter.ScripCriteria;

public class ScripFileRepository extends AbstractCachingFileRepository<Scrip> implements ScripRepository{

	public static final ScripFileRepository INSTANCE = new ScripFileRepository();
	
	private ScripFileRepository() {
		super("scrip");
	}

	public List<Scrip> findByScripCriteria(ScripCriteria scripFilter){
		List<Scrip> scrips = new ArrayList<Scrip>();
		for(Scrip scrip : findAll()){
			if(scrip.getExchangeId().equals(scripFilter.getExchangeId())
					&& scrip.getScripType().equals(scripFilter.getScripType())){
				scrips.add(scrip);
			}
		}
		Collections.sort(scrips);
		return scrips;
	}
	
	@Override
	public Scrip deserialize(String text) {
		return new Scrip(text);
	}

	@Override
	public String serialize(Scrip entity) {
		return entity.toString();
	}
	
}
